#include "form_dzbl.h"
#include "pub_var.h"
#include "ui_form_dzbl.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QSettings>
#include <QMessageBox>
#include <QDebug>
#include <QSqlError>
#include <QSqlRecord>
#include <QDateTime>

Form_dzbl::Form_dzbl(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_dzbl)
{
    ui->setupUi(this);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    //恢复当前窗口位置
    QRect rct = setIni.value("windowState/dzbl").toRect();
    this->setGeometry(rct);

    //数据库连接
    qrm = new QSqlQuery(pub::db_m);
    qro = new QSqlQuery(pub::db_o);
    qrot = new QSqlQuery(pub::db_o);

    //默认日期
    QDate dt;
    ui->deStart->setDate(dt.currentDate());
    ui->deEnd->setDate(dt.currentDate());

    request.setUrl(QUrl(pub::hos_ybjy));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
}

Form_dzbl::~Form_dzbl()
{
    delete ui;
}

void Form_dzbl::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/dzbl", QVariant(this->geometry()));

}

void Form_dzbl::on_pbSearch_clicked()
{


    //getInformation("5943942");
    //return;

    sq = "select card_no, to_char(reg_date, 'yyyy-mm-dd'), dept_name, clinic_code from fin_opr_register "
         " where card_no like '%"
            + ui->leBzh->text()
            + "' and reg_date > to_date('"
            + ui->deStart->date().toString("yyyy-MM-dd")
            +"','yyyy/mm/dd') and reg_date< to_date('"
            + ui->deEnd->date().toString("yyyy-MM-dd")
            +" 23:59:59','yyyy/mm/dd hh24:mi:ss')"
             " order by reg_date" ;
    qro->exec(sq);
    qDebug()<<sq;
    if(!qro->next()){
        QMessageBox::warning(NULL,"注意", "指定范围无数据！ " );
        return;
    }
    ui->lwList->clear();
    QString str;
    while(true){

        str = qro->value(0).toString();
        str.append("\t");
        str.append(qro->value(1).toString());
        str.append("\t");
        str.append(qro->value(2).toString());
        str.append("\t");
        str.append(qro->value(3).toString());
        str.append("\t");

        ui->lwList->addItem(str);

        if(!qro->next()){
            break;
        }
    }

}

void Form_dzbl::on_pbSingle_clicked()
{
    if(ui->rbDzbl->isChecked()){
        dzbl();
    }else if(ui->rbJjjs->isChecked()){
        jjjs();
    }
}


void Form_dzbl::on_pbAll_clicked()
{
//4101,4701
}

void Form_dzbl::on_pbExit_clicked()
{
    this->close();
}

void Form_dzbl::on_lwList_clicked(const QModelIndex &index)
{

    patientInfo.clear();
    ui->ptText->clear();

    getInformation(pub::getStr(ui->lwList->item(index.row())->text(),"\t",4));

}

void Form_dzbl::getInformation(QString clinic_code)
{

    patientInfo["adm_rec_no"] = clinic_code;

    if(clinic_code.isEmpty()){
        QMessageBox::information(this,"注意","clinicCode 为空，无法检索病历!");
        return;
    }

    sq = "select v_out from com_business where clinic_code = '"
         + clinic_code +"' and ywbh = '1101' and v_out is not null order by jysj desc";
qDebug()<<sq;
    qrm->exec(sq);
    qrm->next();

    QString js = qrm->value(0).toString();

    //QJsonDocument jsdc = QJsonDocument::fromJson(js.toLocal8Bit());
QJsonDocument jsdc = QJsonDocument::fromJson(js.toUtf8());

    QJsonObject jsob = jsdc.object();

    personBaseinfo = jsob.value("output").toObject().value("baseinfo").toObject();
    personInsuinfo = jsob.value("output").toObject().value("insuinfo")[0].toObject();
    personIdetinfo = jsob.value("output").toObject().value("idetinfo")[0].toObject();

qDebug()<<personBaseinfo;


sq = "select v_in from com_business where clinic_code = '"
     + clinic_code +"' and ywbh = '2201' and v_in is not null order by jysj desc";
qDebug()<<sq;
qrm->exec(sq);
qrm->next();

js = qrm->value(0).toString();

jsdc = QJsonDocument::fromJson(js.toUtf8());

jsob = jsdc.object();
qDebug()<<"jsob"<<jsob;
person2201 = jsob.value("input").toObject().value("data").toObject();
qDebug()<<"person2201"<<person2201;


    sq = "select v_in from com_business where clinic_code = '"
         + clinic_code +"' and ywbh = '2203A' and v_in is not null order by jysj desc";
qDebug()<<sq;
    qrm->exec(sq);
    qrm->next();


    js = qrm->value(0).toString();
qDebug()<<"js"<<js;
    //jsdc = QJsonDocument::fromJson(js.toLocal8Bit());
      jsdc = QJsonDocument::fromJson(js.toUtf8());

    jsob = jsdc.object();

    person2203 = jsob.value("input").toObject().value("mdtrtinfo").toObject();
    person2203a = jsob.value("input").toObject().value("diseinfo").toArray();
qDebug()<<"person2203"<<person2203;
qDebug()<<"person2203a"<<person2203a;



    sq = "select v_out from com_business where clinic_code = '"
         + clinic_code +"' and ywbh = '2207A' and v_out is not null order by jysj desc";
qDebug()<<sq;
    qrm->exec(sq);
    qrm->next();

    js = qrm->value(0).toString();

    jsdc = QJsonDocument::fromJson(js.toUtf8());

    jsob = jsdc.object().value("output").toObject();

    person2207 = jsob.value("setlinfo").toObject();

qDebug()<<"person2207"<<person2207;

    QMap<QString, QString> pageInfo;
    sq = "select name, sex_code, to_char(birthday,'yyyyMMdd') birth,to_char( reg_date,'yyyy-mm-dd')reg_dt, "
         "dept_name, mcard_no , card_no "
         " from fin_opr_register"
         " where clinic_code = '" + clinic_code + "'";
    qro->exec(sq);
    qro->next();
    patientInfo["name"] = pageInfo["name"] = qro->value(0).toString();
    patientInfo["gend"] = pageInfo["sex"] = qro->value(1).toString() == "M" ? "男" : "女";
    patientInfo["birth"] = pageInfo["birth"] = qro->value(2).toString() ;
    patientInfo["adm_time"] = pageInfo["regdt"] = qro->value(3).toString() ;
    patientInfo["dept_name"] = pageInfo["dpnm"] = qro->value(4).toString() ;
    pageInfo["mcard_no"] = qro->value(5).toString() ;
    patientInfo["mdtrt_sn"] = pageInfo["card_no"] = qro->value(6).toString() ;
    patientInfo["mdtrtsn"] = patientInfo["mdtrt_sn"] ;
    qDebug()<<"检索医师处方内容";
    QString  cur_date, cur_bzh;
    sq="select FEE_CODE,recipe_no,charge_flag,CLINIC_CODE,CARD_NO,to_char(REG_DATE,'yyyy-mm-dd') cdt ,ITEM_CODE,ITEM_NAME,"
       "SPECS,DRUG_FLAG,CLASS_CODE,QTY,ITEM_UNIT,ONCE_DOSE,ONCE_UNIT,FREQUENCY_NAME,USAGE_NAME,EXEC_DPNM,DOCT_NAME,LAB_TYPE,"
       "CHECK_BODY,CLINICDIAG_CODE,CLINICDIAG_NAME,CLINICOTHERDIAG1_CODE,CLINICOTHERDIAG1_NAME,CLINICOTHERDIAG2_CODE,"
       "CLINICOTHERDIAG2_NAME from met_ord_recipedetail where clinic_code='" + clinic_code + "'"
       " order by drug_flag, oper_date";
    if(!qro->exec(sq)){
        QMessageBox::information(this,"error", qro->lastError().text());
        qDebug()<<qro->lastError().text();
        qDebug()<<qro->lastQuery();
        return;
    }
    if(!qro->next()){
        QMessageBox::information(this,"error", "找不到指定检索信息关联的医师处方内容！");
        qDebug()<<qro->lastQuery();
        return;
    }
    pageInfo["cur_date"] = qro->value("cdt").toString();
    pageInfo["cur_bzh"] = qro->value("CARD_NO").toString();
    pageInfo["jzys"] = qro->value("DOCT_NAME").toString();

    while(true){
        //首先检查处方是否执行，然后检查是否退费
        pageInfo["zd"] = qro->value("CLINICDIAG_NAME").toString();
        if(!qro->value("CLINICOTHERDIAG1_NAME").toString().isEmpty()){
            pageInfo["zd"].append("^"+qro->value("CLINICOTHERDIAG1_NAME").toString());
        }
        if(!qro->value("CLINICOTHERDIAG2_NAME").toString().isEmpty()){
            pageInfo["zd"].append("^"+qro->value("CLINICOTHERDIAG2_NAME").toString());
        }
        QString s;
        s = qro->value("charge_flag").toString();
        if(s == "0"){
            s = "（未执行）";
        }else{
            sq=" select count(*) c from pha_com_output where recipe_no = "
               "'"+qro->value("recipe_no").toString()+"' and return_num >0";
            qrot->exec(sq);
            qrot->next();
            if(qrot->value(0).toInt() > 0){
                s = "（已退费）";
            }else{
                s = "";
            }
        }
        int fee_code = qro->value("fee_code").toInt();
        //1 西药 ， 2 中药
        //4 化验 ，7 检查
        //5 处置，
        // 6 留观费 ，10 器械
        //11 采血费， 12 彩色打印照片, 14 取暖费 ，22 床位费（床）

        if( fee_code == 1 || fee_code == 2){ //药品
            s.append(qro->value("ITEM_NAME").toString());
            s.append(" ");
            s.append(qro->value("SPECS").toString());
            s.append(qro->value("QTY").toString());
            s.append(qro->value("ITEM_UNIT").toString());
            s.append("  ");
            s.append(qro->value("USAGE_NAME").toString());
            s.append(" ");
            s.append(qro->value("ONCE_DOSE").toString());
            s.append(qro->value("ONCE_UNIT").toString());
            s.append("/");
            s.append(qro->value("FREQUENCY_NAME").toString());
            if(pageInfo["cljg"].isEmpty()){
                pageInfo["cljg"].append(s);
            }else{
                pageInfo["cljg"].append("\r\n" + s);
            }
        }else if( fee_code == 4 || fee_code == 7){ //化验，检查
            s.append(qro->value("ITEM_NAME").toString());
            s.append(" ");
            s.append(qro->value("QTY").toString());
            s.append(qro->value("ITEM_UNIT").toString());
            if(pageInfo["jcjg"].isEmpty()){
                pageInfo["jcjg"].append(s);
            }else{
                pageInfo["jcjg"].append("\r\n" + s);
            }
        }else if( fee_code == 5){ //处置
            s.append(qro->value("ITEM_NAME").toString());
            s.append(" ");
            s.append(qro->value("QTY").toString());
            s.append(qro->value("ITEM_UNIT").toString());
            if(pageInfo["cljg"].isEmpty()){
                pageInfo["cljg"].append(s);
            }else{
                pageInfo["cljg"].append("\r\n" + s);
            }
        }

        if(!qro->next()){
            break;
        }
    }

    sq="select r.hzzs,r.xbs,r.jws,r.tgjc,r.yz,r.ys ,r.atinc , l.xmmc, l.xmjg, p.lxdh "
       "from ys_bz_e_rec r, ys_bz_e_main m, ys_bz_e_lib l, ys_bz_e_patient_info p"
       " where m.atinc = r.id and  r.rq='"+pageInfo["cur_date"]+"' and m.bzh='"+pageInfo["cur_bzh"]+"'"
       " and l.id=r.id and m.patient_id=p.atinc";

    qrm->exec(sq);

    if(!qrm->next()){
        //bzAtinc = "";
        pageInfo["hzzs"] = "";
        pageInfo["xbs"] = "";
        pageInfo["jws"] = "";
        pageInfo["zzms"] = "";
        pageInfo["yz"] = "";

    }else{
        //bzAtinc = qrm->value("atinc").toString();
        patientInfo["hzzs"] = pageInfo["hzzs"] = qrm->value("hzzs").toString();

        patientInfo["xbs"] = pageInfo["xbs"] = qrm->value("xbs").toString();
        patientInfo["jws"] = pageInfo["jws"] = qrm->value("jws").toString();
        patientInfo["zzms"] = pageInfo["zzms"] = qrm->value("tgjc").toString();
        patientInfo["yz"] = pageInfo["yz"] = qrm->value("yz").toString();
        patientInfo["lxdh"] = pageInfo["lxdh"] = qrm->value("lxdh").toString();
    }

    QString item;

    QStringList ageList = pub::getNl(pageInfo["birth"]);

    item = "姓名：" + pageInfo["name"] + "\t性别："
            + pageInfo["sex"] + "\t年龄：" + ageList[0]+ageList[1] + "\n";
    ui->ptText->insertPlainText( item );

    item = "病志号：" + pageInfo["card_no"] + "\n就诊日期：" + pageInfo["regdt"] + "\n";
    ui->ptText->insertPlainText( item);

    item = "诊室：" + pageInfo["dpnm"] + "\t医保卡号：" + pageInfo["mcard_no"]  + "\n";
    ui->ptText->insertPlainText( item);


    item = "患者主诉：" +  pageInfo["hzzs"] + "\n";
    ui->ptText->insertPlainText( item);

    item = "现病史：" + pageInfo["xbs"] + "\n";
    ui->ptText->insertPlainText( item);

    item = "既往史：" + pageInfo["jws"]  + "\n";
    ui->ptText->insertPlainText( item);

    item = "专科检查：" + pageInfo["zzms"]  + "\n";
    ui->ptText->insertPlainText( item);

    item = "化验检查：" + pageInfo["jcjg"] + "\n";
    ui->ptText->insertPlainText( item);

    item = "诊断：" + pageInfo["zd"]  + "\n";
    item.replace("^","，");
    ui->ptText->insertPlainText( item);

    item = "处置：" +  pageInfo["cljg"] + "\n";
    ui->ptText->insertPlainText( item);

    item = "主治医生：" +  pageInfo["jzys"] + "\n";
    ui->ptText->insertPlainText( item);


}
void Form_dzbl::dzbl()
{
    //
    QJsonObject in;
    if(!putBaseIn(  in, "4701") ){
        return;
    }
    QJsonDocument d;

    QJsonObject inpu,inpud;
    //入院信息
    inpu.insert("mdtrt_sn", patientInfo["adm_rec_no"]);//就医流水号字符型30
    inpu.insert("mdtrt_id", person2207.value("mdtrt_id").toString());//就诊ID字符型30
    inpu.insert("psn_no", personBaseinfo.value("psn_no").toString());//人员编号字符型30
    inpu.insert("mdtrtsn", patientInfo["mdtrtsn"]);//住院号字符型30
    inpu.insert("name", patientInfo["name"]);//姓名字符型50
    inpu.insert("gend", personBaseinfo.value("gend").toString());//性别字符型6
    inpu.insert("adm_rec_no", patientInfo["adm_rec_no"]);//入院记录流水号字符型18
    inpu.insert("wardarea_name", person2201.value("dept_name").toString());//病区名称字符型50
    inpu.insert("dept_code", person2201.value("dept_code").toString());//科室代码字符型30
    inpu.insert("dept_name", person2201.value("dept_name").toString());//科室名称字符型50
    inpu.insert("bedno", "0");//病床号字符型10
    inpu.insert("adm_time", person2203.value("begntime").toString());//入院时间日期时间型
    inpu.insert("illhis_stte_name",  patientInfo["name"]);//病史陈述者姓名字符型50
    inpu.insert("illhis_stte_rltl", "本人");//陈述者与患者关系代码字符型2
    inpu.insert("stte_rele", "1");//陈述内容是否可靠标识字符型1
    inpu.insert("chfcomp", "测试");//主诉字符型100  patientInfo["hzzs"]
    inpu.insert("dise_now", "测试");//现病史字符型100 patientInfo["xbs"]
    inpu.insert("hlcon", "测试");//健康状况字符型3
    inpu.insert("dise_his", "无");//疾病史（含外伤）字符型100
    inpu.insert("ifet","0");//患者传染性标志字符型1
    inpu.insert("ifet_his","无");//传染病史字符型1000
    inpu.insert("prev_vcnt","无");//预防接种史字符型1000
    inpu.insert("oprn_his","无");//手术史字符型1000
    inpu.insert("bld_his","无");//输血史字符型1000
    inpu.insert("algs_his","无");//过敏史字符型1000
    inpu.insert("psn_his","无");//个人史字符型1000
    inpu.insert("mrg_his","已婚");//婚育史字符型1000
    inpu.insert("mena_his","无");//月经史字符型1000
    inpu.insert("fmhis","无");//家族史字符型1000
    inpu.insert("physexm_tprt", 36.5);//体格检查--体温（℃）数值型5,1
    inpu.insert("physexm_pule", 70);//体格检查 -- 脉率（次 /mi数字）数值型3
    inpu.insert("physexm_vent_frqu", 15);//体格检查--呼吸频率字符型3
    inpu.insert("physexm_systolic_pre", 120);//体格检查 -- 收缩压 （mmHg）字符型3
    inpu.insert("physexm_dstl_pre", 80);//体格检查 -- 舒张压 （mmHg）字符型3
    inpu.insert("physexm_height", 175);//体格检查--身高（cm）数值型6，1
    inpu.insert("physexm_wt", 75);//体格检查--体重（kg）数值型7，2
    inpu.insert("physexm_ordn_stas","测试");//体格检查 -- 一般状况 检查结果字符型100
    inpu.insert("physexm_skin_musl","测试");//体格检查 -- 皮肤和黏膜检查结果字符型100
    inpu.insert("physexm_spef_lymph","测试");//体格检查 -- 全身浅表淋巴结检查结果字符型100
    inpu.insert("physexm_head","测试");//体格检查 -- 头部及其器官检查结果字符型100
    inpu.insert("physexm_neck","测试");//体格检查 -- 颈部检查结果字符型100
    inpu.insert("physexm_chst","测试");//体格检查 -- 胸部检查结果字符型100
    inpu.insert("physexm_abd","测试");//体格检查 -- 腹部检查结果字符型100
    inpu.insert("physexm_finger_exam","测试");//体格检查 -- 肛门指诊检查结果描述字符型100
    inpu.insert("physexm_genital_area","测试");//体格检查 -- 外生殖器检查结果字符型100
    inpu.insert("physexm_spin","测试");//体格检查 -- 脊柱检查结果字符型100
    inpu.insert("physexm_all_fors","测试");//体格检查 -- 四肢检查结果字符型100
    inpu.insert("nersys","测试");//体格检查 -- 神经系统检查结果字符型100
    inpu.insert("spcy_info","测试");//专科情况字符型100
    inpu.insert("asst_exam_rslt","测试");//辅助检查结果字符型100
    inpu.insert("rec_doc_code","测试");//接诊医生编号字符型30
    inpu.insert("rec_doc_name","测试");//接诊医生姓名字符型50
    inpu.insert("ipdr_code","测试");//住院医师编号字符型30
    inpu.insert("ipdr_name","测试");//住院医师姓名字符型50
    inpu.insert("chfdr_code","测试");//主任医师编号字符型30
    inpu.insert("chfdr_name","测试");//主任医师姓名字符型50
    inpu.insert("chfpdr_code","测试");//主诊医师代码字符型30
    inpu.insert("chfpdr_name","测试");//主诊医师姓名字符型50
    inpu.insert("main_symp","测试");//主要症状字符型50
    inpu.insert("adm_rea","测试");//入院原因字符型1000
    inpu.insert("adm_way","0");//入院途径字符型1
    inpu.insert("apgr",10);//评分值(Apgar)字符型2
    inpu.insert("diet_info","1");//饮食情况字符型1
    inpu.insert("growth_deg","1");//发育程度字符型1
    inpu.insert("mtl_stas_norm","1");//精神状态正常标志字符型1
    inpu.insert("slep_info","测试");//睡眠状况字符型1000
    inpu.insert("sp_info","测试");//特殊情况字符型1000
    inpu.insert("mind_info","1");//心理状态字符型1
    inpu.insert("nurt","1");//营养状态字符型1
    inpu.insert("self_ablt","1");//自理能力字符型1
    inpu.insert("nurscare_obsv_item_name","测试");//护理观察项目名称字符型30
    inpu.insert("nurscare_obsv_rslt","测试");//护理观察结果字符型1000
    inpu.insert("smoke","0");//吸烟标志字符型1
    inpu.insert("stop_smok_days",0);//停止吸烟天数数值型5
    inpu.insert("smok_info","0");//吸烟状况字符型1
    inpu.insert("smok_day",0);//日吸烟量（支）字符型3
    inpu.insert("drnk","0");//饮酒标志字符型1
    inpu.insert("drnk_frqu","0");//饮酒频率字符型1
    inpu.insert("drnk_day",0);//日饮酒量（mL）数值型3
    inpu.insert("eval_time","0");//评估日期时间数值型
    inpu.insert("resp_nurs_name","测试");//责任护士姓名字符型50
    inpu.insert("vali_flag","1");//有效标志字符型3

    inpu.insert("tcm4d_rslt","测试");//
    inpu.insert("syddclft","测试");//
    inpu.insert("syddclft_name","测试");//
    inpu.insert("prnp_trt","测试");//



    inpud.insert("adminfo",inpu);


    inpu = d.object();
    QJsonArray jsarZd;//诊断



//    diag_dept	诊断科室
//    dise_dor_no	诊断医生编码
//    dise_dor_name	诊断医生姓名


for(int i = 0; i < person2203a.size(); i++ ){

    inpu.insert("inout_diag_type", "2");//出入院诊断类别字符型3Y
    inpu.insert("maindiag_flag","1");//主诊断标志字符型3
    inpu.insert("diag_seq", person2203a[i].toObject().value("diag_srt_no").toInt());//诊断序列号数值型2
    inpu.insert("diag_time", person2203a[i].toObject().value("diag_time").toString());//诊断时间日期时间型
    inpu.insert("wm_diag_code", person2203a[i].toObject().value("diag_code").toString());//西医诊断编码字符型20
    inpu.insert("wm_diag_name", person2203a[i].toObject().value("diag_name").toString());//西医诊断名称字符型100
    //inpu.insert("tcm_dise_code", "");//中医病名代码字符型9
    inpu.insert("vali_flag","1");//有效标志字符型3Y

        jsarZd<<inpu;
}
    inpud.insert("diseinfo", jsarZd);


    inpu = d.object();
    QJsonArray jsarBc;//病程


    inpu.insert("dept_code","测试");//科室代码字符型30
    inpu.insert("dept_name","测试");//科室名称字符型50
    inpu.insert("wardarea_name","测试");//病区名称字符型50
    inpu.insert("bedno","测试");//病床号字符型10
    inpu.insert("rcd_time","2021-09-20 12:00:00");//记录日期时间日期型
    inpu.insert("chfcomp","测试");//主诉字符型100
    inpu.insert("cas_ftur","测试");//病例特点字符型200
    inpu.insert("dise_evid","测试");//诊断依据字符型100
    inpu.insert("dise_plan","测试");//诊疗计划字符型2000
    inpu.insert("ipdr_code","测试");//住院医师编号字符型30
    inpu.insert("ipdr_name","测试");//住院医师姓名字符型50
    inpu.insert("prnt_doc_name","测试");//上级医师姓名字符型50
    inpu.insert("vali_flag","1");//有效标志字符型3Y

    inpud.insert("coursrinfo", jsarBc);


    inpu = d.object();
    QJsonArray jsarSs;//手术

    inpu.insert("oprn_appy_id","测试");//手术申请单号字符型30
    inpu.insert("oprn_time","测试");//手术日期日期型
    inpu.insert("oprn_type_code","测试");//手术分类代码字符型1
    inpu.insert("oprn_oprt_code","测试");//手术操作代码字符型30
    inpu.insert("oprn_oprt_name","测试");//手术操作名称字符型500
    inpu.insert("oprn_begntime","测试");//手术开始时间字符型14
    inpu.insert("oprn_endtime","测试");//手术结束时间字符型14
    inpu.insert("vali_flag","测试");//有效标志字符型3Y

    inpud.insert("oprninfo", jsarSs);


    inpu = d.object();
    QJsonArray jsarQj;//抢救

    inpu.insert("dept","");//科室代码字符型30Y
    inpu.insert("dept_name","");//科室名称字符型50
    inpu.insert("wardarea_name","");//病区名称字符型50
    inpu.insert("bedno","");//病床号字符型10
    inpu.insert("diag_name","");//诊断名称字符型100
    inpu.insert("diag_code","");//诊断代码字符型20
    inpu.insert("cond_chg","");//病情变化情况字符型1000
    inpu.insert("resc_mes","");//抢救措施字符型1000
    inpu.insert("oprn_oprt_code","");//手术操作代码字符型30
    inpu.insert("oprn_oprt_name","");//手术操作名称字符型500
    inpu.insert("oprn_oper_part","");//手术操作部位代码字符型50Y
    inpu.insert("itvt_name","");//介入物名称字符型100
    inpu.insert("oprt_mtd","");//操作方法字符型200
    inpu.insert("oprt_cnt","");//操作次数数值型3,0
    inpu.insert("resc_begntime","");//抢救开始日期时间日期型
    inpu.insert("resc_endtime","");//抢救结束日期时间日期型
    inpu.insert("dise_item_name","");//检查/检验项目名称字符型80
    inpu.insert("dise_ccls","");//检查/检验结果字符型1000
    inpu.insert("dise_ccls_qunt","");//检查/检验定量结果数值型18，4
    inpu.insert("dise_ccls_code","");//检查/检验结果代码字符型1
    inpu.insert("mnan","");//注意事项字符型1000
    inpu.insert("resc_psn_list","");//参加抢救人员名单字符型200
    inpu.insert("proftechttl_code","");//专业技术职务类别代码字符型30
    inpu.insert("doc_code","");//医师编号字符型30
    inpu.insert("dr_name","");//医师姓名字符型50
    inpu.insert("vali_flag","");//有效标志字符型3Y


    inpud.insert("rescinfo", jsarQj);


    inpu = d.object();
    QJsonArray jsarSw;//死亡

    inpu.insert("dept","");//科室代码字符型30
        inpu.insert("dept_name","");//科室名称字符型50
        inpu.insert("wardarea_name","");//病区名称字符型50
        inpu.insert("bedno","");//病床号字符型10
        inpu.insert("adm_time","");//入院时间日期时间型
        inpu.insert("adm_dise","");//入院诊断编码字符型20
        inpu.insert("adm_info","");//入院情况字符型200
        inpu.insert("trt_proc_dscr","");//诊疗过程描述字符型2000
        inpu.insert("die_time","");//死亡时间日期时间型
        inpu.insert("die_drt_rea","");//直接死亡原因名称字符型50
        inpu.insert("die_drt_rea_code","");//直接死亡原因编码字符型10
        inpu.insert("die_dise_name","");//死亡诊断名称字符型50
        inpu.insert("die_diag_code","");//死亡诊断编码字符型20Y
        inpu.insert("agre_corp_dset","");//家属是否同意尸体解剖标志字符型1
        inpu.insert("ipdr_name","");//住院医师姓名字符型50
        inpu.insert("chfpdr_code","");//主诊医师代码字符型30
        inpu.insert("chfpdr_name","");//主诊医师姓名字符型50
        inpu.insert("chfdr_name","");//主任医师姓名字符型50
        inpu.insert("sign_time","");//签字日期时间日期型
        inpu.insert("vali_flag","");//有效标志字符型3Y


    inpud.insert("dieinfo", jsarSw);


    inpu = d.object();
    QJsonArray jsarCy;//出院

    inpu.insert("dscg_date","2021-09-18");//出院日期日期型
    inpu.insert("adm_diag_dscr","测试");//入院诊断描述字符型200
    inpu.insert("dscg_dise_dscr","测试");//出院诊断字符型1000
    inpu.insert("adm_info","测试");//入院情况字符型2000
    inpu.insert("trt_proc_rslt_dscr","测试");//诊治经过及结果（含手术日期名称及结果）字符型2000
    inpu.insert("dscg_info","测试");//出院情况（含治疗效果）字符型2000
    inpu.insert("dscg_drord","测试");//出院医嘱字符型1000
    inpu.insert("caty","测试");//科别字符型6Y
    inpu.insert("rec_doc","测试");//记录医师字符型80
    inpu.insert("main_drug_name","测试");//主要药品名称字符型1500
    inpu.insert("oth_imp_info","测试");//其他重要信息字符型1500
    inpu.insert("vali_flag","1");//有效标志字符型3Y

    inpud.insert("dscginfo", jsarCy);




    in.insert("input", inpud);

    QJsonDocument indoc ;
    indoc.setObject(in);
    qDebug()<<"4701 indoc "<<indoc;

    //保存入参日志
    sq = "update com_business set v_in = '"
            + indoc.toJson(QJsonDocument::Compact)
            + "' where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存入参日志失败！");
        qDebug()<<sq;
    }

    QNetworkReply* reply = manager.post(request,indoc.toJson());

    QEventLoop eventLoop;
    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec(); // 进入等待返回，但ui事件循环依然进行。


    qDebug()<<" request 4701 is returned";

    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    if(!statusCode.isValid()){
        QMessageBox::warning(NULL,"注意","http 请求状态错误: " + statusCode.toString());
        return ;
    }

    QNetworkReply::NetworkError err = reply->error();

    if(err != QNetworkReply::NoError){
        QMessageBox::warning(NULL,"注意",reply->errorString());
        return ;
    }


    QByteArray bt = reply->readAll();
    QString t = QString::fromLocal8Bit(bt);

    QJsonParseError json_error;
    QJsonDocument jsdc = QJsonDocument::fromJson(t.toUtf8(), &json_error);
    if(json_error.error != 0 ){
        QMessageBox::warning(NULL,"注意","返回信息解析错误 " + json_error.errorString());

        return ;
    }
    qDebug()<< "4701 outDoc "<<jsdc;
    QJsonObject jsob = jsdc.object();

//    sq = "update com_business set v_out = '"
//            + jsdc.toJson(QJsonDocument::Compact)
//            + "' where jyls = '" + currentMsgid + "'";

//    if(!qrm->exec(sq)){
//        QMessageBox::information(this,"错误","保存出参日志失败！");
//        qDebug()<<sq;
//    }


    if(jsob.value("infcode").toInt() != 0){
        QString t = jsob.value("err_msg").toString();
        QMessageBox::warning(NULL,"注意","4701 交易失败：" + t );

        return ;
    }
    //ret3201 = jsob.value("output").toObject().value("stmtinfo").toObject();

    //qDebug()<<ret3201.value("setl_optins").toString();

    QMessageBox::information(this,"注意","操作完成。");
                             //+ ret3201.value("stmt_rslt_dscr").toString());
}

void Form_dzbl::jjjs()
{
    QJsonObject in;
    if(!putBaseIn(  in, "4101") ){
        return;
    }
    QJsonDocument d;

    QJsonObject inpu,inpud;

    //setlinfo
    inpu.insert("mdtrt_id", person2207.value("mdtrt_id").toString());//就诊ID字符型30Y
    inpu.insert("setl_id", person2207.value("setl_id").toString());//结算ID字符型30Y
    inpu.insert("fixmedins_name", pub::hos_name);//定点医药机构名称字符型200Y
    inpu.insert("fixmedins_code", pub::hos_ybbh);//定点医药机构编号字符型12Y
    inpu.insert("medcasno", patientInfo["mdtrt_sn"]);//病案号字符型40Y
qDebug()<<"patientInfo[card_no]"<<patientInfo["mdtrt_sn"];
    inpu.insert("psn_name", patientInfo["name"]);//人员姓名字符型50Y
    inpu.insert("gend", personBaseinfo.value("gend").toString());//性别字符型6YY
    inpu.insert("brdy", personBaseinfo.value("brdy").toString());//出生日期日期型Y
    inpu.insert("age", personBaseinfo.value("age").toDouble());//
    inpu.insert("ntly", "CN");//国籍字符型6YY
    inpu.insert("naty", personBaseinfo.value("naty").toString());//民族字符型3YY
    inpu.insert("patn_cert_type", personBaseinfo.value("psn_cert_type").toString());//患者证件类别字符型3YY
    inpu.insert("certno", personBaseinfo.value("certno").toString());//证件号码字符型50Y
    inpu.insert("prfs", "90");//职业字符型6Y　Y
    inpu.insert("coner_name", patientInfo["name"]);//联系人姓名字符型50Y
    inpu.insert("patn_rlts", "01");//与患者关系字符型6Y　Y
    inpu.insert("coner_addr", "0");//联系人地址字符型200Y
    inpu.insert("coner_tel", "13511112222");//联系人电话字符型50Y
    inpu.insert("hi_type", "310");//医保类型字符型3Y　Y
    inpu.insert("insuplc", personInsuinfo.value("insuplc_admdvs").toString());//参保地字符型6Y
    inpu.insert("ipt_med_type", "");//住院医疗类型字符型3Y　Y
    inpu.insert("adm_caty", "");//入院科别字符型6Y　Y
    inpu.insert("refldept_dept", "");//转科科别字符型6Y
    inpu.insert("dscg_caty", "");//出院科别字符型6Y　Y
    inpu.insert("maindiag_flag", "");//主诊断标志字符型3YY
    inpu.insert("bill_code","111");//票据代码字符型50Y
    inpu.insert("bill_no","002");//票据号码字符型30Y
    inpu.insert("biz_sn","001");//业务流水号字符型50Y
    inpu.insert("setl_begn_date", person2207.value("setl_time").toString());//结算开始日期日期型Y
    inpu.insert("setl_end_date", person2207.value("setl_time").toString());//结算结束日期日期型Y
    inpu.insert("psn_selfpay",  0);//个人自付数值型16,2Y
    inpu.insert("psn_ownpay", 0);//个人自费数值型16,2Y
    inpu.insert("acct_pay", 0);//个人账户支出数值型16,2Y
    inpu.insert("psn_cashpay", 0);//个人现金支付数值型16,2Y
    inpu.insert("hi_paymtd", "1");//医保支付方式字符型3Y　Y
    inpu.insert("hsorg", "");//医保经办机构字符型100Y
    inpu.insert("hsorg_opter", "");//医保机构经办人字符型50Y
    inpu.insert("medins_fill_dept", "沈阳和平仁晟门诊部");//定点医疗机构填报部门字符型100Y
    inpu.insert("medins_fill_psn", "测试");//定点医疗机构填报人字符型50Y

    inpud.insert("setlinfo",inpu);




    //payinfo
    inpu = d.object();
    QJsonArray jsarPayinfo;

    inpu.insert("fund_pay_type", "310100");//基金支付类型字符型6　Y　Y
    inpu.insert("fund_payamt", 10);//基金支付金额数值型16,2　Y


    jsarPayinfo<<inpu;

    inpud.insert("payinfo", jsarPayinfo);

    //iteminfo收费项目
    inpu = d.object();
    QJsonArray jsarIteminfo;

    inpu.insert("med_chrgitm", "09");//医疗收费项目字符型6YY
    inpu.insert("amt", 10);//金额数值型16,2Y
    inpu.insert("claa_sumfee", 10);//甲类费用合计数值型16,2Y
    inpu.insert("clab_amt", 0);//乙类金额数值型16,2Y
    inpu.insert("fulamt_ownpay_amt", 0);//全自费金额数值型16,2Y
    inpu.insert("oth_amt", 0);//其他金额数值型16,2Y

    jsarIteminfo<<inpu;

    inpud.insert("iteminfo", jsarIteminfo);

/*
    //opspdiseinfo门慢
    inpu.insert("参数代码","");//参数名称参数类型参数长度代码标识是否必填
    inpu.insert("diag_name","");//病种名称字符型100Y
    inpu.insert("diag_code","");//病种代码字符型20Y
    inpu.insert("oprn_oprt_name","");//手术操作名称字符型500Y
    inpu.insert("oprn_oprt_code","");//手术操作代码字符型30Y
    inpu.insert("maindiag_flag","");//主诊断标志字符型3YY

    //diseinfo住院
    inpu.insert("diag_type","");//诊断类别字符型3　Y　Y
    inpu.insert("diag_code","");//诊断代码字符型20　Y
    inpu.insert("diag_name","");//诊断名称字符型100　Y
    inpu.insert("maindiag_flag","");//主诊断标志字符型3YY


    //oprninfo手术
    inpu.insert("oprn_oprt_type","");//手术操作类别字符型3YY
    inpu.insert("oprn_oprt_name","");//手术操作名称字符型500Y
    inpu.insert("oprn_oprt_code","");//手术操作代码字符型30Y
    inpu.insert("oprn_oprt_date","");//手术操作日期日期型Y
    inpu.insert("anst_way","");//麻醉方式字符型6Y
    inpu.insert("oper_dr_name","");//术者医师姓名字符型50Y
    inpu.insert("oper_dr_code","");//术者医师代码字符型20Y
    inpu.insert("oprn_oprt_begnendtime","");//手术及操作起止时间日期型Y
    inpu.insert("anst_begnendtime","");//麻醉起止时间日期型Y

    //icuinfoicu
    inpu.insert("scs_cutd_ward_type","");//重症监护病房类型字符型6YY
    inpu.insert("scs_cutd_inpool_time","");//重症监护进入时间日期时间型Y
    inpu.insert("scs_cutd_exit_time","");//重症监护退出时间日期时间型Y
    inpu.insert("scs_cutd_sum_dura","");//重症监护合计时长字符型10Y
*/
    //inpud.insert()
//    in.insert("input", inpud);

    in.insert("input", inpud);

    QJsonDocument indoc ;
    indoc.setObject(in);

    qDebug()<<"4101 indoc "<<indoc;

    //保存入参日志
    sq = "update com_business set v_in = '"
            + indoc.toJson(QJsonDocument::Compact)
            + "' where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存入参日志失败！");
        qDebug()<<sq;
    }

    QNetworkReply* reply = manager.post(request,indoc.toJson());

    QEventLoop eventLoop;
    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec(); // 进入等待返回，但ui事件循环依然进行。


    qDebug()<<" request 4101 is returned";

    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    if(!statusCode.isValid()){
        QMessageBox::warning(NULL,"注意","http 请求状态错误: " + statusCode.toString());
        return ;
    }

    QNetworkReply::NetworkError err = reply->error();

    if(err != QNetworkReply::NoError){
        QMessageBox::warning(NULL,"注意",reply->errorString());
        return ;
    }


    QByteArray bt = reply->readAll();
    QString t = QString::fromLocal8Bit(bt);

    QJsonParseError json_error;
    QJsonDocument jsdc = QJsonDocument::fromJson(t.toUtf8(), &json_error);
    if(json_error.error != 0 ){
        QMessageBox::warning(NULL,"注意","返回信息解析错误 " + json_error.errorString());

        return ;
    }
    qDebug()<< "4101 outDoc "<<jsdc;
    QJsonObject jsob = jsdc.object();

    sq = "update com_business set v_out = '"
            + jsdc.toJson(QJsonDocument::Compact)
            + "' where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
      QMessageBox::information(this,"错误","保存出参日志失败！");
        qDebug()<<sq;
    }


    if(jsob.value("infcode").toInt() != 0){
        QString t = jsob.value("err_msg").toString();
        QMessageBox::warning(NULL,"注意","4101 交易失败：" + t );

        return ;
    }
    //ret3201 = jsob.value("output").toObject().value("stmtinfo").toObject();

    //qDebug()<<ret3201.value("setl_optins").toString();

    QMessageBox::information(this,"注意","结算信息上传完成。");

}

bool Form_dzbl::putBaseIn( QJsonObject &in, QString code)
{
    QString inp;
    inp = pub::getYbStrJ(code,"");   //
    if(inp.isEmpty()){
        //pub::useInsurance = false;  //设置医保不可用
        QMessageBox::information(this,"note","申请交易流水号失败！");
        return false; //未能成功申请交易流水号
    }

    currentMsgid = inp;

    in.insert("infno", code);
    in.insert( "msgid", inp);
    in.insert("mdtrtarea_admvs", pub::hos_insuplc);
    //in.insert("insuplc_admdvs", mpPerson["insuplc_admdvs"] );

    in.insert("insuplc_admdvs","210100");
    in.insert("recer_sys_code", "OwnSoft");
    in.insert("dev_no","");
    in.insert("dev_safe_info","");
    in.insert("cainfo", pub::hos_secretcode);
    in.insert("signtype","");
    in.insert("infver", "V1.0");
    in.insert("opter_type", "1");
    in.insert("opter", pub::usr_code);
    in.insert("opter_name", pub::usr_name);
    in.insert("inf_time", QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
    in.insert("fixmedins_code", pub::hos_ybbh);
    in.insert("fixmedins_name", pub::hos_name);
    in.insert("sign_no", pub::sign_no);

    return true;
}
